﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace ElingOA6_DSJCJ.DAL
{
    public partial class FTP
    {
        public FTP()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_FTP");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = FID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ElingOA6_DSJCJ.Model.FTP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_FTP(");
            strSql.Append("FDeptNo,FLinkMan,FLinkTel,FSubject,FInputTime,FEntityNo,FEmplNo,FFTPType)");
            strSql.Append(" values (");
            strSql.Append("@FDeptNo,@FLinkMan,@FLinkTel,@FSubject,@FInputTime,@FEntityNo,@FEmplNo,@FFTPType)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('t_FTP')");
        //    strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FLinkMan", SqlDbType.NVarChar,30),
					new SqlParameter("@FLinkTel", SqlDbType.NVarChar,30),
					new SqlParameter("@FSubject", SqlDbType.NVarChar,255),
					new SqlParameter("@FInputTime", SqlDbType.DateTime),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FFTPType", SqlDbType.NVarChar,20),
                    new SqlParameter("@returnBId", SqlDbType.Int)};
            parameters[0].Value = model.FDeptNo;
            parameters[1].Value = model.FLinkMan;
            parameters[2].Value = model.FLinkTel;
            parameters[3].Value = model.FSubject;
            parameters[4].Value = model.FInputTime;
            parameters[5].Value = model.FEntityNo;
            parameters[6].Value = model.FEmplNo;
            parameters[7].Value = model.FFTPType;
            parameters[8].Direction = ParameterDirection.Output;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
    
            model.FID =decimal.Parse( parameters[8].Value.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool InsertdocAttach(Model.DocAttach attachObject ,string Fid)
        {

            StringBuilder strSql = new StringBuilder();
                    strSql.Append("insert into t_docAttach(");
                    strSql.Append("FAttachSvrFileName,FAttachDesc,FAttachContent,FAttachType,FAttachSource,FAttachFileExt,FEntityNo,FAttachOldFileName)");
                    strSql.Append(" values (");
                    strSql.Append("@FAttachSvrFileName,@FAttachDesc,@FAttachContent,@FAttachType,@returnBId,@FAttachFileExt,@FEntityNo,@FAttachOldFileName)");
                    strSql.Append(";select IDENT_CURRENT('t_docAttach')");
                    SqlParameter[] parameters1 = {
					new SqlParameter("@FAttachSvrFileName", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachDesc", SqlDbType.NVarChar,255),
					new SqlParameter("@FAttachContent", SqlDbType.Image),
					new SqlParameter("@FAttachType", SqlDbType.NVarChar,80),
					new SqlParameter("@returnBId", SqlDbType.Decimal,9),
					new SqlParameter("@FAttachFileExt", SqlDbType.NVarChar,80),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FAttachOldFileName", SqlDbType.NVarChar,255)};
                    parameters1[0].Value = attachObject.FAttachSvrFileName;
                    parameters1[1].Value = attachObject.FAttachDesc;
                    //parameters1[2].Value = attachObject.FAttachContent;
                    parameters1[2].Value = DBNull.Value;
                    parameters1[3].Value = attachObject.FAttachType;
                    parameters1[4].Value = Fid;
                    parameters1[5].Value = attachObject.FAttachFileExt;
                    parameters1[6].Value = attachObject.FEntityNo;
                    parameters1[7].Value = attachObject.FAttachOldFileName;
                    int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters1);

                   
                    if (rows > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_DSJCJ.Model.FTP model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_FTP set ");
            strSql.Append("FLinkMan=@FLinkMan,");
            strSql.Append("FLinkTel=@FLinkTel,");
            strSql.Append("FSubject=@FSubject,");
            strSql.Append("FInputTime=@FInputTime,");
            strSql.Append("FFTPType=@FFTPType");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FLinkMan", SqlDbType.NVarChar,30),
					new SqlParameter("@FLinkTel", SqlDbType.NVarChar,30),
					new SqlParameter("@FSubject", SqlDbType.NVarChar,255),
					new SqlParameter("@FInputTime", SqlDbType.DateTime),
					new SqlParameter("@FFTPType", SqlDbType.NVarChar,20),
					new SqlParameter("@FID", SqlDbType.Decimal,9),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FLinkMan;
            parameters[1].Value = model.FLinkTel;
            parameters[2].Value = model.FSubject;
            parameters[3].Value = model.FInputTime;
            parameters[4].Value = model.FFTPType;
            parameters[5].Value = model.FID;
            parameters[6].Value = model.FDeptNo;
            parameters[7].Value = model.FEntityNo;
            parameters[8].Value = model.FEmplNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP ");
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = FID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_FTP ");
            strSql.Append(" where FID in (" + FIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.FTP GetModel(decimal FID)
        {

            StringBuilder strSql = new StringBuilder();
           // strSql.Append("select  top 1 FID,FDeptNo,FLinkMan,FLinkTel,FSubject,FInputTime,FEntityNo,FEmplNo,FFTPType from t_FTP ");
            strSql.Append("select  top 1 FID,t_FTP.FDeptNo,FLinkMan,FLinkTel,FSubject,FInputTime,t_FTP.FEntityNo,FEmplNo,FFTPType,FDeptName  from t_FTP INNER JOIN dbo.t_HRDept ON t_HRDept.FDeptNo = t_FTP.FDeptNo AND t_HRDept.FEntityNo = t_FTP.FEntityNo ");
         
            strSql.Append(" where FID=@FID");
            SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.Decimal)
			};
            parameters[0].Value = FID;

            ElingOA6_DSJCJ.Model.FTP model = new ElingOA6_DSJCJ.Model.FTP();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.FTP DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.FTP model = new ElingOA6_DSJCJ.Model.FTP();
            if (row != null)
            {
                if (row["FID"] != null && row["FID"].ToString() != "")
                {
                    model.FID = decimal.Parse(row["FID"].ToString());
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FLinkMan"] != null)
                {
                    model.FLinkMan = row["FLinkMan"].ToString();
                }
                if (row["FLinkTel"] != null)
                {
                    model.FLinkTel = row["FLinkTel"].ToString();
                }
                if (row["FSubject"] != null)
                {
                    model.FSubject = row["FSubject"].ToString();
                }
                if (row["FInputTime"] != null && row["FInputTime"].ToString() != "")
                {
                    model.FInputTime = DateTime.Parse(row["FInputTime"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FFTPType"] != null)
                {
                    model.FFTPType = row["FFTPType"].ToString();
                }
                //if (row.Table.Columns.Contains("FCodeName"))
                //{
                //    if (row["FCodeName"] != null)
                //    {
                //        model.FCodeName = row["FCodeName"].ToString();
                //    }

                //}
                if (row.Table.Columns.Contains("FDeptName"))
                {
                    if (row["FDeptName"] != null)
                    {

                        model.FDeptName = row["FDeptName"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FID,FDeptNo,FLinkMan,FLinkTel,FSubject,FInputTime,FEntityNo,FEmplNo,FFTPType ");
            strSql.Append(" FROM t_FTP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
         public DataSet GetFtpAndDocAttach(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM dbo.t_FTP LEFT JOIN dbo.t_docAttach ON dbo.t_FTP.FID=dbo.t_docAttach.FAttachSource AND dbo.t_docAttach.FAttachType='FTP' WHERE FAttachSvrFileName IS NOT NULL");
        
            if (strWhere.Trim() != "")
            {
                strSql.Append( strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
         public bool FIsAllowLookFtpByEmpNoAndNoAgency(string FEmplNo, string FEntityNo)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append(@" SELECT  count(*)  FROM  dbo.t_HRDept WHERE  FIsAgency='N' AND FDeptNo IN (SELECT  FDeptNo FROM dbo.v_HRDeptEmpl   WHERE FEmplNo=@FEmplNo AND  FEntityNo=@FEntityNo  )");
             SqlParameter[] parameters = {
										new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)

					};

             parameters[0].Value = FEmplNo;
             parameters[1].Value = FEntityNo;
             return DbHelperSQL.Exists(strSql.ToString(), parameters);
         }
       public bool FIsAllowLookFtpOrAllowByDept(string Fid ,string FEmplNo ,string FEntityNo)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append(@" SELECT sum(cols) FROM ( SELECT count(*) as cols FROM dbo.t_ftp WHERE FID=@Fid AND FDeptNo in (SELECT FDeptNo FROM dbo.v_HREmpl WHERE FEntityNo=@FEntityNo AND FEmplNo=@FEmplNo )" +
 @"UNION  all SELECT  count(*) as cols FROM  dbo.t_HREmpl WHERE FEmplNo=@FEmplNo AND  FEntityNo=@FEntityNo  AND FIsAllowLookFtp='Y') a");
             SqlParameter[] parameters = {
					new SqlParameter("@Fid", SqlDbType.Int,6),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
					};
             parameters[0].Value = Fid;
             parameters[1].Value =FEmplNo;
             parameters[2].Value = FEntityNo;
             return DbHelperSQL.Exists(strSql.ToString(),parameters);
         }
       public bool FIsAllowLookFtpOrAllowByDeptnew(string Fid, string FEmplNo, string FEntityNo)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@"

SELECT sum(cols) FROM ( SELECT count(*) as cols FROM dbo.V_ftp WHERE FID=@Fid AND( ( FDeptNo in (SELECT FDeptNo FROM dbo.v_HREmpl WHERE FEntityNo=@FEntityNo AND FEmplNo=@FEmplNo )) OR (EXISTS( SELECT * FROM dbo.V_ftp WHERE FID=@Fid AND FDeptNo IN (SELECT FDeptNo FROM dbo.t_HRDept WHERE FIsAgency='N')) AND  EXISTS(SELECT FDeptNo FROM dbo.v_HREmpl WHERE FEntityNo=@FEntityNo AND FEmplNo=@FEmplNo AND FIsAgency='N' ) ))
UNION  all SELECT  count(*) as cols FROM  dbo.t_HREmpl WHERE FEmplNo=@FEmplNo AND  FEntityNo=@FEntityNo  AND FIsAllowLookFtp='Y') a
            ");
           SqlParameter[] parameters = {
					new SqlParameter("@Fid", SqlDbType.Int,6),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
					};
           parameters[0].Value = Fid;
           parameters[1].Value = FEmplNo;
           parameters[2].Value = FEntityNo;
           return DbHelperSQL.Exists(strSql.ToString(), parameters);
       }



       public bool FIsAllowLookFtpByEmpNo(string FEmplNo, string FEntityNo)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@" SELECT  count(*)  FROM  dbo.t_HREmpl WHERE FEmplNo=@FEmplNo AND  FEntityNo=@FEntityNo  AND FIsAllowLookFtp='Y'");
           SqlParameter[] parameters = {
										new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)

					};

           parameters[0].Value = FEmplNo;
           parameters[1].Value = FEntityNo;
           return DbHelperSQL.Exists(strSql.ToString(), parameters);
       }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FID,FDeptNo,FLinkMan,FLinkTel,FSubject,FInputTime,FEntityNo,FEmplNo,FFTPType ");
            strSql.Append(" FROM t_FTP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_FTP ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FID desc");
            }
            strSql.Append(")AS Row, T.*  from t_FTP T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_FTP";
            parameters[1].Value = "FID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

